//
// Created by LiuYou on 2021/5/12.
//

/*
 * 操作
 *      assign() 注意
 *      vec[i]
 *      vec.at[i]
 *
 *      front()和back()返回的元素如果使用引用类型接受的话, 改变这个引用, 原来的值也会改变; 如果使用值类型接受的话, 改变这个值类型不会改变原来的值。
 *      front()和back()返回的是元素的值, 因为它在容器中是不销毁的, 所以可以使用引用接受, 也可以使用值接受返回这两个函数的返回值。
 *      
 *
 *
 */
#include <vector>
#include <iostream>
#include "Book.hpp"


void test_assign();

void test_front_and_back();

int main() {
//    test_assign();

    test_front_and_back();


    return 0;
}

void test_front_and_back() {
    std::vector<Book> vec;
    Book book = Book("moon", "留攸");
    std::cout << "vec.capacity(): " << vec.capacity() << std::endl;
    std::cout << "vec.size(): " << vec.size() << std::endl;
    vec.push_back(book);
    std::cout << "vec.capacity(): " << vec.capacity() << std::endl;
    std::cout << "vec.size(): " << vec.size() << std::endl;
    vec.push_back(book);
    std::cout << "vec.capacity(): " << vec.capacity() << std::endl;
    std::cout << "vec.size(): " << vec.size() << std::endl;
    vec.push_back(book);
    std::cout << "vec.capacity(): " << vec.capacity() << std::endl;
    std::cout << "vec.size(): " << vec.size() << std::endl;

    for (const auto& item : vec) {
        std::cout << item << std::endl;
    }
    Book& front = vec.front();
    front.setName("不冲");
    Book& back = vec.back();
    back.setName("不冲");
    std::cout << "======================" << std::endl;
    for (const auto& item : vec) {
        std::cout << item << std::endl;
    }

    Book book1 = vec.front();
    book1.setName("干");
    std::cout << book1 << std::endl;
    std::cout << "======================" << std::endl;
    for (const auto& item : vec) {
        std::cout << item << std::endl;
    }
    vec.pop_back();
    vec.pop_back();
    std::cout << std::endl;
    std::cout << vec.front() << std::endl;
    std::cout << vec.back() << std::endl;
    // 运行结果:
//    /home/lucas/CLionProjects/CppStandardLibrary-Linux/cmake-build-debug/VectorTest03
//    vec.capacity(): 0
//    vec.size(): 0
//    vec.capacity(): 1
//    vec.size(): 1
//    vec.capacity(): 2
//    vec.size(): 2
//    vec.capacity(): 4
//    vec.size(): 3
//    name: moon author: 留攸
//    name: moon author: 留攸
//    name: moon author: 留攸
//    ======================
//    name: 不冲 author: 留攸
//    name: moon author: 留攸
//    name: 不冲 author: 留攸
//    name: 干 author: 留攸
//    ======================
//    name: 不冲 author: 留攸
//    name: moon author: 留攸
//    name: 不冲 author: 留攸
//
//    name: 不冲 author: 留攸
//    name: 不冲 author: 留攸
//
//    Process finished with exit code 0


}

void test_assign() {
    std::vector<Book> vec;
    vec.reserve(100);
    std::cout << "vec.capacity(): " << vec.capacity() << std::endl;
    std::cout << "vec.size(): " << vec.size() << std::endl;
    Book book = Book("chong", "liuyou");
    vec.push_back(book);
    vec.push_back(book);
    vec.push_back(book);
    vec.push_back(book);
    std::cout << "vec.capacity(): " << vec.capacity() << std::endl;
    std::cout << "vec.size(): " << vec.size() << std::endl;
    for (const auto& item : vec) {
        std::cout << item << std::endl;
    }

    vec.assign(1, Book("冲", "留攸"));
    std::cout << "vec.capacity(): " << vec.capacity() << std::endl;
    std::cout << "vec.size(): " << vec.size() << std::endl;
    for (const auto& item : vec) {
        std::cout << item << std::endl;
    }
    // 运行结果:
//    /home/lucas/CLionProjects/CppStandardLibrary-Linux/cmake-build-debug/VectorTest03
//    vec.capacity(): 100
//    vec.size(): 0
//    vec.capacity(): 100
//    vec.size(): 4
//    name: chong author: liuyou
//    name: chong author: liuyou
//    name: chong author: liuyou
//    name: chong author: liuyou
//    vec.capacity(): 100
//    vec.size(): 1
//    name: 冲 author: 留攸
//
//    Process finished with exit code 0


}
